package chat

import akka.actor.ActorSystem
import akka.actor.Props
import akka.cluster.Cluster

object Main {
  def main(args: Array[String]): Unit = {
    val systemName = "ChatApp"
    val system1 = ActorSystem(systemName)
    val joinAddress = Cluster(system1).selfAddress //在system外围wrap一个Cluster，就会对应一个地址
    println("system 1 joinAddr:" + joinAddress)

    Cluster(system1).join(joinAddress) //try to join this cluster node with node specified by address
    system1.actorOf(Props[MemberListener], "memberListener")
    system1.actorOf(Props[RandomUser], "Ben")
    system1.actorOf(Props[RandomUser], "Kathy")

    Thread.sleep(5000)

    val system2 = ActorSystem(systemName)
    println("system 2 adddr:" + Cluster(system2).selfAddress)
    Cluster(system2).join(joinAddress)
    system2.actorOf(Props[RandomUser], "Skye")

    /*
    Thread.sleep(10000)
    val system3 = ActorSystem(systemName)
    Cluster(system3).join(joinAddress)
    system3.actorOf(Props[RandomUser], "Miguel")
    system3.actorOf(Props[RandomUser], "Tyler")
    */
  }
}
